package com.zle.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zle.pojo.Person;
//import com.zle.services.demo.Istu;
@Controller
@RequestMapping("/person")
public class StuController {
	
	private static Logger logger = LoggerFactory.getLogger(StuController.class);
	
//	@Autowired
//	private Istu stu;
	/**
	 * 限制只能get访问;
	 * @param name
	 * @param model
	 * @return
	 */
//	@RequestMapping(value="/view",method=RequestMethod.GET)
//	public String showView(@RequestParam("pname") String name,Model model){
//		model.addAttribute("name", stu.getName());
//		logger.error(name);
//		return "/hello/stu";
//	}
	/**
	 * @param name
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/view2/{pname}",method=RequestMethod.GET)
	public String showView2(@PathVariable("pname") Integer name,Map<String,Object> model){
		model.put("name", name);
//		logger.error(name.toString());
		return "/hello/stu";
	}
	/**
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view3")
	public String showView3(HttpServletRequest request){
		logger.error("view3");
		logger.error(request.getParameter("pname"));
		request.setAttribute("name", "qwertyu");
		return "/hello/stu";
	}
	@RequestMapping(value="/view5")
	public String goIndex(){
		return "/hello/index";
	}
	/**
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view4")
	public String showView4(Person request){
		System.out.println(ReflectionToStringBuilder.toString(request));
		return "redirect:view3";
	}
	@RequestMapping(value="/uploadshow")
	public String uploadShow(){
		
		return "/hello/upload";
	}
	/**
	 * @param file
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(@RequestParam("file")MultipartFile file,Model model) throws IOException{
		System.out.println(file.getName());
		System.out.println(file.getOriginalFilename());
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File("D:/uoload",file.getOriginalFilename()));
		model.addAttribute("result","success!");
		return "/hello/upload";
	}
	
	@RequestMapping("/json")
	public @ResponseBody() Object jsonView(){
		Person p = new Person();
		p.setAge("1");
		p.setName("zle");
		p.setSex("1");
		return p;
	}
	
}
